package com.buddy.learn.algorithm.exam;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Buddy
 * @date 2021/6/8
 */
public class SolutionXM2 {

    /**
     * 榜单
     * {
     * name：
     * score：
     * }
     * <p>
     * 张三 1000
     * abc 10000
     * <p>
     * 从100万用户里，取前50名，按score排序
     * <p>
     * 数据库里
     *
     * @param args
     */
    public static void main(String[] args) {

        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            User user = new User(i + "", i);
            userList.add(user);
        }


        List result = userList.stream()
                .sorted((o1, o2) -> o2.getScore() - o1.getScore())
                .limit(50)
                .collect(Collectors.toList());
        result.parallelStream().forEach(user->{
            System.out.println(JSON.toJSONString(user));
        });
//        System.out.println(JSON.toJSONString(result));

        List result1 = userList.parallelStream()
                .sorted((o1, o2) -> o2.getScore() - o1.getScore())
                .limit(50)
                .collect(Collectors.toList());
        System.out.println("---------------------------------------------------------------");
//        System.out.println(JSON.toJSONString(result1));
        result1.stream().forEach(user->{
            System.out.println(JSON.toJSONString(user));
        });
    }

}

class User {
    private String name;
    private int score;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    User(String name, int score) {
        this.name = name;
        this.score = score;
    }
}